package com.cf.timer;

import com.cf.BindThreadPool;
import com.cf.game.service.schedule.CircleCapableTimingWheel;
import com.cf.game.service.thread.ThreadPool;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * cf-game-CowBoyTimingWheel
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/3/27 13:39
 */
@Slf4j
public class StageTimingWheel {

    private static int tickDuration = 1000;

    private static int timeoutInMillis = 30 * 1000;

    protected static CircleCapableTimingWheel<ExpirationTask> timingWheel;

    static {
        timingWheel = new CircleCapableTimingWheel<>(tickDuration, timeoutInMillis / tickDuration, TimeUnit.MILLISECONDS);
        timingWheel.addExpirationListener(expirationTask -> BindThreadPool.getInstance().run(expirationTask));
        timingWheel.start();
    }

    public static void addTask(ExpirationTask expirationTask, Integer second) {
        timingWheel.add(expirationTask, second * 1000L);
    }

    public static void addTask(ExpirationTask expirationTask, Long timeout) {
        timingWheel.add(expirationTask, timeout);
    }


}
